package com.gcit.seniorWish.web.modular.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.exception.GunsException;
import com.gcit.seniorWish.core.node.SelectNode;
import com.gcit.seniorWish.core.util.ToolUtil;
import com.gcit.seniorWish.web.common.constant.dictmap.NoticeDict;
import com.gcit.seniorWish.web.common.constant.enums.NoticeTypeEnum;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.common.persistence.model.Notice;
import com.gcit.seniorWish.web.core.annotion.BussinessLog;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.core.log.LogObjectHolder;
import com.gcit.seniorWish.web.core.shiro.ShiroKit;
import com.gcit.seniorWish.web.modular.system.service.INoticeService;
import com.gcit.seniorWish.web.modular.system.warpper.NoticeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author fengshuonan
 * @Date 2017-05-09 23:02:21
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {

	private String PREFIX = "/system/notice";

	@Autowired
	private INoticeService noticeService;

	/**
	 * 跳转到通知列表首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "/notice.html";
	}

	/**
	 * 跳转到添加通知
	 */
	@RequestMapping("/noticeAdd")
	public String noticeAdd(Model model) {
		model.addAttribute("noticeTypes", noticeService.listBatchTypeForSelect2(null));
		return PREFIX + "/noticeAdd.html";
	}

	/**
	 * 跳转到修改通知
	 */
	@RequestMapping("/noticeUpdate/{noticeId}")
	public String noticeUpdate(@PathVariable String noticeId, Model model) {
		Notice notice = this.noticeService.selectById(noticeId);
		model.addAttribute("notice", notice);
		model.addAttribute("noticeTypes", noticeService.listBatchTypeForSelect2(null));
		LogObjectHolder.me().set(notice);
		return PREFIX + "/noticeInfo.html";
	}

	/**
	 * 跳转到首页通知
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/hello")
	public String hello() {
		List<Notice> notices = noticeService.selectList(Condition.create().eq("type", NoticeTypeEnum.NOTICE.getVal()));
		super.setAttr("noticeList", notices);
		return "/blackboard.html";
	}

	/**
	 * 获取通知列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		EntityWrapper<Notice> ew = new EntityWrapper();
		if (StrUtil.isNotBlank(condition)) {
			ew.like("title", condition);
		}
		ew.orderBy("create_time", false);
		List<Map<String, Object>> list = noticeService.selectMaps(ew);
		return super.warpObject(new NoticeWrapper(list));
	}

	/**
	 * 新增通知
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	@BussinessLog(value = "新增通知", key = "title", dict = NoticeDict.class)
	public Object add(Notice notice) {
		if (ToolUtil.isOneEmpty(notice, notice.getTitle(), notice.getContent())) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		notice.setCreateBy(ShiroKit.getUser().getId());
		notice.setCreateTime(new Date());
		notice.insert();
		return super.SUCCESS_TIP;
	}

	/**
	 * 删除通知
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	@BussinessLog(value = "删除通知", key = "noticeId", dict = NoticeDict.class)
	public Object delete(@RequestParam String noticeId) {

		// 缓存通知名称
		LogObjectHolder.me().set(ConstantFactory.me().getNoticeTitle(noticeId));

		this.noticeService.deleteById(noticeId);

		return SUCCESS_TIP;
	}

	/**
	 * 修改通知
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	@BussinessLog(value = "修改通知", key = "title", dict = NoticeDict.class)
	public Object update(Notice notice) {
		if (ToolUtil.isOneEmpty(notice, notice.getId(), notice.getTitle(), notice.getContent())) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		Notice old = this.noticeService.selectById(notice.getId());
		old.setTitle(notice.getTitle());
		old.setContent(notice.getContent());
		old.setType(notice.getType());
		old.updateById();
		return super.SUCCESS_TIP;
	}

	/**
	 * 查询通知类型列表
	 * 
	 * @author tonglu
	 * @date 2018-03-19 下午 4:01
	 */
	@RequestMapping(value = "/listBatchTypeForSelect2")
	@ResponseBody
	public Object listBatchTypeForSelect2() {
		Page<SelectNode> page = new Page();
		page.setRecords(noticeService.listBatchTypeForSelect2(null));
		return packForSelect2(page);
	}
}
